package com.elitel.frame.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.elitel.frame.base.service.JarServerConfigService;
import com.elitel.frame.core.commonInterface.BaseJarInter;
import com.elitel.frame.main.dao.BaseJarinfoMapper;
import com.elitel.frame.main.dao.BaseJarmethodMapper;
import com.elitel.frame.main.dao.BaseJarserverMapper;
import com.elitel.frame.main.dao.CfgDbMapper;
import com.elitel.frame.main.dao.CfgExtjarMapper;
import com.elitel.frame.main.dao.ext.ExtBaseJarinfoMapper;
import com.elitel.frame.main.dao.ext.ExtCfgExtjarMapper;
import com.elitel.frame.main.entity.BaseJarinfo;
import com.elitel.frame.main.entity.BaseJarmethod;
import com.elitel.frame.main.entity.BaseJarserver;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.CfgExtjar;
import com.elitel.frame.main.entity.ext.Census;
import com.elitel.frame.main.entity.ext.ExtCfgExtjar;
@Service
public class JarServerConfigServiceImpl implements JarServerConfigService,BaseJarInter {

	@Resource
	private CfgDbMapper cfgDbMapper;
	
	@Resource
	private ExtCfgExtjarMapper extCfgExtjarMapper; 
	
	@Resource
	private BaseJarinfoMapper baseJarinfoMapper;
	
	@Resource
	private BaseJarmethodMapper baseJarmethodMapper;
	
	@Resource
	private BaseJarserverMapper baseJarserverMapper;
	
	@Resource
	private ExtBaseJarinfoMapper extBaseJarinfoMapper;
	
	private static final Object sqlObj = null;
	
	@Override
	public List<Census> getJarServerTree() {
		List<CfgDb> list = cfgDbMapper.selectAllDb();
		List<Census> censusList = new ArrayList<>();
		for(CfgDb cfDb:list){
			Census census = new Census();
			census.setPid("0");
			census.setName(cfDb.getDbMark()+"("+cfDb.getDbName()+")");
			census.setId(cfDb.getDbKey());
			List<CfgExtjar> jarList = extCfgExtjarMapper.selectByDBKey(cfDb.getDbKey());
			List<Census> lista = new ArrayList<>();
			for(CfgExtjar cExtjar:jarList){
				Census censuss = new Census();
				censuss.setPid(cfDb.getDbKey());
				censuss.setId(cExtjar.getGuid());
				censuss.setName(cExtjar.getServicename());
				lista.add(censuss);
			}
			census.setChildren(lista);
			censusList.add(census);
		}
		return censusList;
	}

	@Override
	public String insertJarServer(ExtCfgExtjar extCfgExtjar) {
		String message = "fail";
		CfgExtjar cfgExtjar = new CfgExtjar();
		cfgExtjar.setGuid(extCfgExtjar.getGuid());
		cfgExtjar.setExtjarid(extCfgExtjar.getExtjarid());
		cfgExtjar.setServicename(extCfgExtjar.getServicename());
		cfgExtjar.setCallinvoke(extCfgExtjar.getCallinvoke());
		cfgExtjar.setRemark(extCfgExtjar.getRemark());
		cfgExtjar.setExtjarclass(extCfgExtjar.getExtjarclass());
		cfgExtjar.setDbKey(extCfgExtjar.getDbKey());
		cfgExtjar.setDtSql(extCfgExtjar.getDtSql());
		int a = extCfgExtjarMapper.insertSelective(cfgExtjar);
		BaseJarserver baseJarserver = new BaseJarserver();
		baseJarserver.setGuid(extCfgExtjar.getGuid());
		baseJarserver.setJarSerName(cfgExtjar.getServicename());
		baseJarserver.setJarName(extCfgExtjar.getJarname());
		baseJarserver.setJarPackageName(extCfgExtjar.getRemark());
		baseJarserver.setJarClassName(extCfgExtjar.getExtjarclass());
		baseJarserver.setJarMethodName(extCfgExtjar.getCallinvoke());
		baseJarserver.setDbKey(extCfgExtjar.getDbKey());
		baseJarserver.setQueryList(extCfgExtjar.getDtSql());
		baseJarserver.setAttach(extCfgExtjar.getOthersql());
		if (a>0) {
			baseJarserverMapper.insert(baseJarserver);
			message="success";
		}
		return message;
	}

	@Override
	public String updateJarServer(CfgExtjar cfgExtjar) {
		String message = "fail";
		int a = extCfgExtjarMapper.updateByPrimaryKey(cfgExtjar);
		if (a>0) {
			message = "success";
		}
		return message;
	}

	@Override
	public String deleteJarServer(String guid) {
		String message = "fail";
		int a = extCfgExtjarMapper.deleteByPrimaryKey(guid);
		if (a>0) {
			message = "success";
		}
		return message;
	}

	@Override
	public List<BaseJarinfo> selectAllJarInfo() {
		List<BaseJarinfo> list = baseJarinfoMapper.selectAll();
		return list;
	}

	@Override
	public List<BaseJarmethod> getPackageList(String sguid) {
		List<BaseJarmethod> list = baseJarmethodMapper.getPackageList(sguid);
		return list;
	}

	@Override
	public List<BaseJarmethod> getClassList(String sguid) {
		List<BaseJarmethod> list = baseJarmethodMapper.getClassList(sguid);
		return list;
	}

	@Override
	public int selectCountGUID(String guid) {
		int a = extCfgExtjarMapper.selectCountGUID(guid);
		return a;
	}

	@Override
	public List<BaseJarmethod> getclassmethod(String classname) {
		List<BaseJarmethod> list  = baseJarmethodMapper.getclassmethod(classname);
		return list;
	}

	@Override
	public CfgExtjar selectJarOne(String guid) {
		CfgExtjar cfgExtjar = extCfgExtjarMapper.selectByPrimaryKey(guid);
		return cfgExtjar;
	}

	@Override
	public Object beforeMethod(Object jsonResult) {
		System.out.println("实现执行前的方法");
		return null;
	}
	
	@Override
	public Object afterMethod(Object jsonResult) {
		System.out.println("实现执行后的方法");
		String message = "实现执行后的方法";
		return message;
	}
	
	public String jarTest(Object object,Object object2){
		String testMessage = "zheshishigeceshifangfa";
		return testMessage;
	}		
	
	@Override
	public BaseJarserver selectServer(String serviceID) {
		BaseJarserver baseJarserver = baseJarserverMapper.selectByPrimaryKey(serviceID);
		return baseJarserver;
	}


	@Override
	public BaseJarinfo selectInfo(String jarName) {
		BaseJarinfo baseJarinfo = extBaseJarinfoMapper.selectJarInfo(jarName);
		return baseJarinfo;
	}

	@Override
	public BaseJarserver selectBaseJarServer(String guid) {
		BaseJarserver baseJarserver = baseJarserverMapper.selectByPrimaryKey(guid);
		return baseJarserver;
	}

}
